<?php

namespace app\api\model\order;

use app\logic\common\model\Channel as logicChannel;
use think\Exception;
use think\facade\Log;
use think\facade\Queue;
use think\Model;
use ulit\thirdPayment\SafePay;

class Order extends Model
{


    protected $json=['third_ext_json','pay_ext_json','pay_error_json'];
    protected $jsonAssoc=true;
    public static function submitOrder($arr){

        (new Order)->startTrans();
        try {
            self::create($arr);
            (new Order)->commit();
            return true;
        }catch (Exception $exception){

            (new Order)->rollback();


            Log::write(json_encode($exception->getMessage()));
            return false;
        }
    }


    /****
     * @param $order_sn
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function checkStatusByOrderSn($order_sn){
       // $order_check=SafePay::getInstance()->query($order_sn);

      //  $result= json_decode($order_check,true);


//         $end_time=date('Y-m-d H:i:30',strtotime('+10 seconds'));
//        cache('timer',$end_time);
//        $result['result']['orderStatus']=2;
        $rand=rand(1,5);

        if($rand!=2){

            $params=[
                'order_sn'=>$order_sn,
            ];
            Queue::later(3, 'app\logic\job\CanLangPay',$params); //延迟2s
        }

        if($rand=2){
            return array(
                'orderStatus' => 1,
                'msg' =>'success'
            );
        }

        return array(
            'orderStatus' =>2,
            'msg' => 'no'
        );

//        return array(
//            'code' => $result['result']['orderStatus'],
//            'msg' => $result['result']['remark']
//        );
    }
}